--------------------------
--      Coin FX v2      --
--  by: EeveeEuphoria   --
--------------------------

--[[ Guide
		Adding the API
	Add this to the start of your global level luna lua file for it to work everywhere:
	coinFX = require("coinFX")

	Or for a per-level basis, use this in the relevant level luna lua file:
	local coinFX = require("coinFX")

		Function
	coinFX.addCoin(NUM, GFX, FRAME, X, Y) will spawn NUM amount of coins in the HUD, each one appearing after the other consecutively, and each playing a random noise when they spawn.
	Each argument after NUM is optional, and does not need to be provided to function.
	If GFX is provided, then it will use that as a basis for the coin graphic. It should be formatted like so: coinFX.addCoin(5, "coolcoinGFX"), with the files being "coolcoinGFX0.png", ""coolcoinGFX1.png", "coolcoinGFX2.png", "coolcoinGFX3.png"
	If FRAME is provided, it will specify how many frames the GFX has, assuming the starting position is 0 (so, if you have 4 frames, it'd be 4 minus 1, 3.).
	If X/Y is provided, it will modify the X and/or Y position for that coin element alone, and not affect any other coins being shown.

		Options
	coinFX.x and coinFX.y adjusts the starting position of the coins, by default it's 34 and 30, and will be set to that if nothing is provided.
	coinFX.priority sets the render priority for the coin graphics. If not set, it will default to 3.
	coinFX.direction1 and coinFX.direction2 will set the random directions the coins will fly. By default they are 0, 5, which will make the range go from straight up, to the right.
		If you get an error with this, ensure you have both coinFX.direction1 and coinFX.direction2 set, it won't work properly unless both are set.
		Make sure the numbers aren't too big, otherwise they'll go flying at light speed off the screen!
	coinFX.opaque will start the coins to be opaque all the way through. If not set, coins will start with transparency, gradually fading back to solid.

		Files this script uses & will try to look for
	Graphics: coinFX/coin0.png, coinFX/coin1.png, coinFX/coin2.png, coinFX/coin3.png
	Sounds: coinFX/coin1.ogg, coinFX/coin2.ogg, coinFX/coin3.ogg
]]

--[[ TODO:
	- Re-write to allow different GFX to be used within the addCoin function
]]

--[[ CHANGELOG:
	- Made it so HUD elements pause when the game is paused.
]]

local coinFX = {}

coinFX.x = 34
coinFX.y = 30
coinFX.priority = 3
coinFX.direction1 = 0
coinFX.direction2 = 5

function coinFX.onInitAPI()
	registerEvent(coinFX, "onDraw")
end

local coinsT = {}

function coinFX.onDraw()
	for key, value in pairs(coinsT) do
		if not coinsT[key].delay then
			if not Misc.isPaused() then
				coinsT[key].frame = coinsT[key].frame - 1
				if coinsT[key].frame < 0 then
					coinsT[key].frame = 50
				end
				if (coinsT[key].frame % 5 == 0) then
					coinsT[key].realframe = coinsT[key].realframe + 1
					local TrueFrameCount = 3
					if coinsT[key].frameamnt then
						TrueFrameCount = coinsT[key].frameamnt
					end
					if coinsT[key].realframe > TrueFrameCount then
						coinsT[key].realframe = 0
					end
				end
				if coinsT[key].accely < 15 then
					coinsT[key].accely = coinsT[key].accely + .8
				end
				if coinsT[key].rand > 0 then
					coinsT[key].rand = coinsT[key].rand - .05
				end
				if coinsT[key].opacity < 1 then
					coinsT[key].opacity = coinsT[key].opacity + 0.05
				end
				coinsT[key].x = coinsT[key].x + coinsT[key].rand
				coinsT[key].y = coinsT[key].y + coinsT[key].accely
			end
			local CoinGFX
			if coinsT[key].graphic then
				CoinGFX = Graphics.loadImage(Misc.resolveFile(coinsT[key].graphic .. coinsT[key].realframe .. ".png"))
			else
				CoinGFX = Graphics.loadImage(Misc.resolveFile("coinFX/coin" .. coinsT[key].realframe .. ".png"))
			end
			Graphics.drawImageWP(CoinGFX, coinsT[key].x, coinsT[key].y, coinsT[key].opacity, coinsT[key].prority)
		elseif not Misc.isPaused() then
			coinsT[key].delay = coinsT[key].delay - 1
			if coinsT[key].delay < 0 then
				coinsT[key].delay = nil
				local coinsfx = Misc.resolveFile("coinFX/coin" .. RNG.randomInt(1, 3) .. ".ogg")
				Audio.playSFX(coinsfx)
			end
		end
		if coinsT[key].y > 600 then -- exceeded past the screen's limits, delete from the table
			coinsT[key] = nil
		end
	end
end

function coinFX.AddCoin(amnt, graphic, frame, thex, they)
	for i=amnt,1,-1 do
		local myCoin = {}
		myCoin.x = coinFX.x
		myCoin.y = coinFX.y
		if thex then
			myCoin.x = thex
		end
		if they then
			myCoin.y = they
		end

		myCoin.prority = coinFX.priority
		myCoin.accely = RNG.randomInt(-15, -10) + RNG.random()
		myCoin.rand = RNG.randomInt(coinFX.direction1, coinFX.direction2) + RNG.random()
		myCoin.frame = 50
		myCoin.realframe = 0
		if coinFX.opaque then
			myCoin.opacity = 1
		else
			myCoin.opacity = 0
		end
		myCoin.delay = 1
		if i then
			myCoin.delay = (RNG.randomInt(10, 18)) * (i/2)
		end
		if graphic then
			myCoin.graphic = graphic
		end
		if frame then
			myCoin.frameamnt = frame
		end
		table.insert(coinsT, myCoin)
	end
end

return coinFX